/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.validation;

import db.entities.Referenze;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import logger.Log;

/**
 *
 * @author marcy
 */
public class MailChangeBean {

    private String newMail;
    private int patientId;

    public String getNewMail() {
        return newMail;
    }

    public void setNewMail(String newMail) {
        this.newMail = newMail;
    }


    public int getPatientId() {
        return patientId;
    }

    public void setPatientId(int patientId) {
        this.patientId = patientId;
    }
    
    public String cambiaMail(){
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        Referenze r = null;
        
        //Creo la query
        TypedQuery<Referenze> query = em.createNamedQuery("Referenze.findByIdPaz", Referenze.class);
        try {
            r = query.setParameter("idPaz", getPatientId()).getSingleResult();
        } catch (NoResultException ex) {
            Log.getLogger().error("Problema con la query per il Cambio MAIL");
            return null;
        } finally {
            em.getTransaction().begin();
                r.setEmail(newMail);
            em.getTransaction().commit();
        }
        return "successo";
        
        
        
        
    }
}
